<?php
// +----------------------------------------------------------------------
// | 日志管理
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: PostsAction.class.php 121 2011-05-17 10:03:49Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class PostsAction extends BaseAction {

	public function index() {
		redirect(__APP__);
	}
	
	//发表日志
	public function addPosts() {
		$cat  =  D('Category');
		$tag  =  D('Tag');
		$user =  D('AdminUser');
		$cat_option  =  $cat->getCatList(0,0,0,true);
		$tag_name    =  $tag->getTagName(false);
		$nick_name   =  $user->getUserNickName(true);
		//获取最大上传限制
		$allow_max_size = @ini_get('upload_max_filesize'); // 单位为m
		$allow_max_size_b = return_bytes(@ini_get('upload_max_filesize')); // 单位为b
		
		$this->assign('action_link', array('href'=>__URL__.'/listPosts/', 'text' => L('listPosts')));
		$this->assign('ur_here',L('addPosts'));
		$this->assign('cat_option', $cat_option);
		$this->assign('tag_name', $tag_name);
		$this->assign('nick_name', $nick_name);
		$this->assign('allow_max_size', $allow_max_size);
		$this->assign('allow_max_size_b', $allow_max_size_b);
		$this->display();	
	}

	//发表日志
	public function insertPosts() {
		if ($_REQUEST['act']) {	
			$posts = D('Posts');
			$data = $posts->create();
			if (false === $data) {
				$this->error($posts->getError());
			} else {
				if ($result = $posts->insertPostsTag($data)) {
					clear_cache_files();    // 清除缓存
					set_log('add(Posts, Id: '.$result.')');
					$this->assign('jumpUrl',__URL__.'/listPosts/');
					$this->success(L('success'));
				} else {
					$this->error(L('error'));
				}
			}
		} else {
			$this->error (L('illegal'));
		}	
	}
	
	//日志列表
	public function listPosts() {
		//排序字段 默认为主键
		if (isset($_REQUEST ['order'])) {
			$order = $_REQUEST ['order'];
		} else {
			$order = 'posts_id';
		}
		//排序方式 默认按照倒序排列
		if (isset($_REQUEST ['sort'])) {
			$sort = $_REQUEST ['sort'];
		} else {
			$sort = 'desc';
		}
		//查询
		$map  =	 array();		
		$map['posts_status']  =	 1;
		$map['posts_type']    =	 1;
		if ($_REQUEST['catid']) {
			$map['category_id'] = $_REQUEST['catid'];
		}	
		if ($_REQUEST['category_id']) {
			$map['category_id'] = $_REQUEST['category_id'];
		}
		if ($_REQUEST['posts_author']) {
			$map['posts_author'] = $_REQUEST['posts_author'];
		}	
		if ($_REQUEST['keyword']!='') {
			$map['posts_title'] = array('like',"%{$_REQUEST['keyword']}%");
		}		
		//列表
		$posts  =  D('Posts');
		$cat    =  D('Category');
		$user   =  D('AdminUser');
		//$tag    =  D('Tag');
		$cat_option  =  $cat->getCatList(0,0,0,true);
		$nick_name   =  $user->getUserNickName(true);
		//$tag_name    =  $tag->getTagName(true);
		
		import("@.ORG.Page");
		C ( 'PAGE_ROLLPAGE',C('page_roll') );	
		$count = $posts->where($map)->count();
		$p = new Page ($count, C('admin_list_num'));	
		$list = $posts->where($map)->field('posts_id,posts_author,posts_title,posts_url,posts_date,posts_tracknum,posts_comment,
		posts_draft,posts_stick,posts_istrack,posts_hit,posts_allowcom,posts_addtime,category_id')->relation(true)
		->order("`".$order."` ".$sort)->limit($p->firstRow.','.$p->listRows)->findAll();
		$p->setConfig('theme',C('admin_page_theme'));
		//分页跳转的时候保证查询条件
		foreach ($map as $key => $val) {
			if (!is_array ($val)) {
				$p->parameter .= "$key=" . urlencode ( $val ) . "&";
			}
		}
		//dump($list);
		$page = $p->show ();

		$sort = $sort.'_'.$order;
		
		$this->assign('action_link', array('href'=>__URL__.'/addPosts/', 'text' => L('addPosts')));
		$this->assign('ur_here',L('listPosts'));
		$this->assign('cat_option', $cat_option);
		$this->assign('nick_name', $nick_name);
		//$this->assign('tag_name', $tag_name);
		$this->assign('pagestyle',C('admin_page_style'));
		$this->assign('page', $page);
		$this->assign('list',$list);
		$this->assign('sort',$sort);
		$this->display();
	}
	
	//扔进回收站
	public function trashPosts() {
		$map	=	array();
		$map['posts_id']	 =	$_REQUEST['id'];
		$posts = M('Posts');
		if ($list = $posts->where($map)->setField('posts_status', 0)) {
			clear_cache_files();    // 清除缓存
			set_log('trash(Posts, Id: '.$_REQUEST['id'].')');
			$this->assign('jumpUrl',__URL__.'/listPosts/');
			$this->success(L('success'));
		} else {
			$this->error(L('error'));
		}
	}
	
	public function editPosts() {
		$posts = D('Posts');
		$id   = $_REQUEST['id'];
		$vo	  = $posts->getByPostsId($id);
		
		$cat  =  D('Category');
		$tag  =  D('Tag');
		$user =  D('AdminUser');
		$cat_option   = $cat->getCatList(0,$vo['category_id'] ,0,true);	
		$pt_name    =  $tag->getTagFromEditPosts($id);
		$tag_name    =  $tag->getTagName(false);
		$nick_name   =  $user->getUserNickName(true,$vo['posts_author']);
		
		//获取最大上传限制
		$allow_max_size = @ini_get('upload_max_filesize'); // 单位为m
		$allow_max_size_b = return_bytes(@ini_get('upload_max_filesize')); // 单位为b

		$this->assign('action_link', array('href'=>__URL__.'/listPosts/', 'text' => L('listPosts')));
		$this->assign('ur_here',L('editPosts'));
		$this->assign('cat_option', $cat_option);
		$this->assign('nick_name', $nick_name);
		$this->assign('pt_name', $pt_name);
		$this->assign('tag_name', $tag_name);
		$this->assign('vo',$vo);
		$this->assign('allow_max_size', $allow_max_size);
		$this->assign('allow_max_size_b', $allow_max_size_b);
		$this->display();
	}	
	
	public function updatePosts() {
		$posts = D('Posts');
		$data = $posts->create();
		if (!$data) {
			$this->error($posts->getError());
		} else {
			if ($posts->updataPostsTag($data)) {
				clear_cache_files();    // 清除缓存
				set_log('edit(Posts, Id: '.$data['posts_id'].')');
				$this->assign('jumpUrl',__URL__.'/listPosts/');
				$this->success(L('success'));
			} else {
				$this->error(L('error'));
			}
		}
	}	
	
	public function dropTagFromEditPosts() {
		$map  =	 array();	
		$map['posts_id'] = $_REQUEST['postsId'];
		$map['tag_id']   = $_REQUEST['tagId'];
		$pt = M ('PostsTag');
		$list = $pt->where($map)->delete();
		if ($list!==false) {
			clear_cache_files();    // 清除缓存
			set_log('edit(Posts, drop, Tag, Id: '.$_REQUEST['postsId'].')');
			redirect(__URL__.'/editPosts/id/'.$_REQUEST['postsId']);
		} else {
			$this->error (L('error'));
		}

	}
	
	//批量操作
	public function batchPosts() {
	}
	
}

?>